﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;

namespace Acms.SqlDal
{
    public class EmailActivationCode : Base
    {
        public enum Fields
        {
            Id,
            UserId,
            Code,
            EmailAddress,
            Activated,
            Created
        }

        protected override void ReadRecord(System.Collections.Hashtable table, System.Data.SqlClient.SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.UserId, reader, table);
            GetReaderValue<string>(Fields.Code, reader, table);
            GetReaderValue<string>(Fields.EmailAddress, reader, table);
            GetReaderValue<DateTime?>(Fields.Activated, reader, table);
            GetReaderValue<DateTime>(Fields.Created, reader, table);
        }

        public Hashtable GetById(int id)
        {
            var cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByCodeAndSiteId(string code, int siteId)
        {
            var cmd = CreateCommand("GetByCodeAndSiteId");
            AddInputParameter(cmd, "Code", code, SqlDbType.VarChar, 10);
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllByUserId(int userId)
        {
            var cmd = CreateCommand("GetAllByUserId");
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable GetByUserIdAndCode(int userId, string code)
        {
            var cmd = CreateCommand("GetByUserIdAndCode");
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "Code", code, SqlDbType.VarChar, 10);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllByUserIdAndEmail(int userId, string emailAddress)
        {
            var cmd = CreateCommand("GetAllByUserIdAndEmail");
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "EmailAddress", emailAddress, SqlDbType.VarChar, 255);
            return ExecuteGetCollection(cmd);
        }

        public void Update(ref int id, int userId, string code, string emailAddress, DateTime? activated, DateTime created, ref object concurencyId)
        {
            var cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "Code", code, SqlDbType.VarChar, 10);
            AddInputParameter(cmd, "EmailAddress", emailAddress, SqlDbType.VarChar, 255);
            AddInputParameter(cmd, "Activated", activated, SqlDbType.DateTime);
            AddInputParameter(cmd, "Created", created, SqlDbType.DateTime);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);

            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            base.ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            var cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            base.ExecuteCommand(cmd);
        }
    }
}
